<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Reading extends CI_Controller {
	public $client = '';
	public function __construct() {
		parent::__construct();
		$this->output->set_header("Content-type: text/html; charset=utf-8");
		$this->load->model('Comm');
		$this->client = 'web/';
		$arrUserLogin = json_decode($this->Comm->getUserCookie(), true);
		$this->arrData['user_login'] = $arrUserLogin;
		$this->arrData['rid'] = isset($this->uri->rsegments[4]) ? $this->uri->rsegments[4] : NULL;
		$this->arrData['user_info'] = $this->arrData['prid'] = NULL;
		if(!empty($arrUserLogin)) {
			$this->arrData['user_info'] = $this->Comm->getUserInfo($arrUserLogin['uid']);
			$this->arrData['prid'] = $this->Comm->getOne('pay_reading', 'prid', array('uid'=>$arrUserLogin['uid'], 'rid'=>$this->arrData['rid']));
		}
	}
	
	public function index() {
		$this->arrData['html5'] = NULL;
		$this->comm_reader();
	}
	
	public function html5() {
		$this->arrData['html5'] = '1';
		$this->comm_reader();
	}
	
	public function comm_reader() {
		$rid = $this->arrData['rid'];
		if($rid <= 0 || !is_numeric($rid)) {
			echo json_encode(array('error'=>'error_rid'));	
			return;
		}
		$this->load->library('user_agent');
		$ie6 = false;
		if($this->agent->browser() == 'Internet Explorer' && $this->agent->version() < 7) {
			$ie6 = true;
		}
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
		$this->arrData['channel'] = $this->Comm->getChannel();
		$channel = isset($this->uri->rsegments[3]) ? $this->uri->rsegments[3] : NULL;
		$this->arrData['selfchannel'] = $this->Comm->getChannel($this->arrData['channel'], 0, $channel);
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
		
		$this->arrData['ie6'] = $ie6;
		$this->arrData['reading'] = $this->Comm->getRow('reading', '*', array('rid'=>$rid));
		if(!empty($this->arrData['reading'])) {};
		$this->arrData['ower_user'] = $this->Comm->getRow('user_info', 'nick_name,my_pic', array('uid'=>$this->arrData['reading']['uid']));
		//
		$sql = "INSERT IGNORE INTO user_reading_history(uid, own_uid, rid) VALUES('"
		       .$this->arrData['user_login']['uid']."','".$this->arrData['reading']['uid']."','$rid');";
		$this->Comm->query($sql);
		$this->Comm->set('view_times', 'view_times+1', false);
		$this->Comm->update('reading', NULL, array('rid'=>$rid));
		//
		$this->arrData['channelnav'] = $this->Comm->getChannel($this->arrData['channel'], $this->arrData['selfchannel']['parent']);
	    $this->arrData['__META']    = $this->Comm->getMeta($this->arrData['reading']['title'] . ',' . $this->arrData['selfchannel']['title'].'频道');
		$this->load->view($this->client . 'reader', $this->arrData);	
	}
	
	public function pay() {
		$rid = $this->input->get('rid');
		if($rid <= 0 || !is_numeric($rid)) {
			echo json_encode(array('error'=>'error_rid'));	
			return;
		}
		if(empty($this->arrData['user_login'])) {
			$this->load->helper('url');
			redirect('login.html?referer=' . urlencode($this->Comm->getSelfUrl()) . '&msg='.urlencode('你还没有登录！'), 'location', 302);
			//echo json_encode(array('error'=>'nologin'));	
			return;
		}
		$rid = $this->input->get('rid');
		$uid = $this->arrData['user_login']['uid'];
		$my_money = ($this->arrData['user_info']['my_money'] + $this->arrData['user_info']['my_donate_money']);
		$arrReading = $this->Comm->getRow('reading', 'uid, price', array('rid'=>$rid));
		$this->arrData['siteinfo'] = $this->Comm->getTableCache('siteinfo', array('sid'=>1), true);
		$this->arrData['not_enough_money'] = NULL;
		if($my_money < $arrReading['price']) {
			//echo json_encode(array('error'=>'not_enough_money'));
			$this->arrData['message'] = '您的'.$this->arrData['siteinfo']['site_gold_name'].'不够，可以进行充值。';
			$this->arrData['not_enough_money'] = 1;
			//return;
		} else {
			$prid = $this->Comm->getOne('pay_reading', 'prid', array('rid'=>$rid, 'uid'=>$uid));
			if($prid > 0) {
				$this->arrData['message'] = '您已经支付过这篇文档，无需再次支付！';
			} else {
				if($arrReading['price'] <= $this->arrData['user_info']['my_donate_money']) {
					$arrUpdate = array('my_donate_money'=>($this->arrData['user_info']['my_donate_money'] - $arrReading['price']));	
				} elseif(empty($this->arrData['user_info']['my_donate_money'])) {
					$arrUpdate = array('my_money'=>($this->arrData['user_info']['my_money'] - $arrReading['price']));	
				} else {
					$arrUpdate = array('my_donate_money'=>0, 'my_money'=>($my_money - $arrReading['price']));	
				}
				$this->Comm->insert('pay_reading', array('uid'=>$uid, 'rid'=>$rid, 'own_uid'=>$arrReading['uid'], 'price'=>$arrReading['price']));
				$this->Comm->update('user_info', $arrUpdate, array('uid'=>$uid));
				$this->Comm->getUserInfo($uid, true);
				$this->Comm->set('pay_times', 'pay_times+1', false);
				$this->Comm->update('reading', NULL, array('rid'=>$rid));
				//echo json_encode(array('error'=>'0', 'success'=>'pay_success'));
				$this->arrData['message'] = '支付成功，您可以下载或者阅读这篇文档！';
			}
		}
		$this->arrData['__META'] = $this->Comm->getMeta('支付');
		$this->load->view($this->client . 'pay', $this->arrData);	
	}
	
	public function get() {
		$rid = $this->input->get('rid');
		if($rid <= 0 || !is_numeric($rid)) {
			echo json_encode(array('error'=>'error_rid'));	
			return;
		}
		if(empty($this->arrData['user_login'])) {
			$this->load->helper('url');
			redirect('login.html?referer=' . urlencode($this->Comm->getSelfUrl()) . '&msg='.urlencode('需要登录才能下载！'), 'location', 302);
			//echo json_encode(array('error'=>'nologin'));	
			return;
		}
		$uid = $this->arrData['user_login']['uid'];
		$arrReading = $this->Comm->getRow('reading', 'uid, read_host, file_path, price, doc_type', array('rid'=>$rid));
		if(empty($arrReading['price'])) {
		} else {
			$arrPay_reading = $this->Comm->getRow('pay_reading', 'uid, price', array('uid'=>$uid, 'rid'=>$rid));
			if(empty($arrPay_reading)) {
				echo json_encode(array('error'=>'no_pay'));	
				return;	
			}
		}
		$sql = "INSERT IGNORE INTO user_reading_download(uid, own_uid, rid) VALUES('".$this->arrData['user_login']['uid']."','".$arrReading['uid']."','$rid');";
		$this->Comm->query($sql);
		$this->Comm->set('down_times', 'down_times+1', false);
		$this->Comm->update('reading', NULL, array('rid'=>$rid));
		$this->load->library('download');
		$strHost = substr($arrReading['read_host'], 7);
		$strHost = substr($strHost, strpos($strHost, '/') + 1);
		$file = $strHost . $arrReading['file_path'].'.'.$arrReading['doc_type'];
		$result = $this->download->fileResume($file);
		if($result == false) echo json_encode(array('error'=>$file . ' file_not_exist'));	
		return;
	}
	public function addfaver() {
		$rid = $this->input->get('rid');
		if($rid <= 0 || !is_numeric($rid)) {
			echo json_encode(array('error'=>'error_rid'));	
			return;
		}
		if(empty($this->arrData['user_login'])) {
			$this->load->helper('url');
			redirect('login.html?referer=' . urlencode($this->Comm->getSelfUrl()) . '&msg='.urlencode('你还没有登录！'), 'location', 302);
			//echo json_encode(array('error'=>'nologin'));	
			return;
		}
		$sql = "INSERT IGNORE INTO user_reading_faver(uid, rid) VALUES('".$this->arrData['user_login']['uid']."','$rid');";
		$this->Comm->query($sql);
		$this->arrData['message'] = '加入收藏成功！';
		$rid = $this->input->get('rid');
		$uid = $this->arrData['user_login']['uid'];
		$this->arrData['__META'] = $this->Comm->getMeta('加入收藏夹');
		$this->load->view($this->client . 'addfaver', $this->arrData);	
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */